package com.cheejee.goodscenter.util;

import static com.cheejee.goodscenter.util.ResponseCode.BAD_PARAM;

import javax.validation.ConstraintViolationException;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.cheejee.goodscenter.module.response.BaseReponse;
import com.cheejee.goodscenter.service.exception.CheckException;
import com.cheejee.goodscenter.service.exception.DataNotExistException;
import com.cheejee.goodscenter.service.exception.DuplicateException;
import com.cheejee.goodscenter.service.exception.SystemError;

/**
 * 
 * @author CARRY
 * @Date 2020年10月22日下午3:46:58
 */
//@RestControllerAdvice
public class ExceptionCatch {

	@ExceptionHandler(value = CheckException.class)
	public BaseReponse checkHandler(CheckException e) {
		return new BaseReponse(BAD_PARAM, e.getCheckMessage());
	}

	@ExceptionHandler(value = ConstraintViolationException.class)
	public BaseReponse validatedHandler(ConstraintViolationException e) {
		return new BaseReponse(BAD_PARAM, e.getConstraintViolations().iterator().next().getMessage());
	}
	
	@ExceptionHandler(value = DuplicateException.class)
	public BaseReponse DuplicateHandler(DuplicateException e) {
		return new BaseReponse(ResponseCode.DUPLICATE_FIELD, e.getMessage());
	}
	
	
	@ExceptionHandler(value = DataNotExistException.class)
	public BaseReponse DataNotExistHandler(DataNotExistException e) {
		return new BaseReponse(ResponseCode.DATA_NOT_EXIST, e.getMessage());
	}
	
	@ExceptionHandler(value = SystemError.class)
	public BaseReponse SystemErrorHandler(SystemError e) {
		return new BaseReponse(ResponseCode.SYSTEM_ERROR, e.getMessage());
	}
	
	
}
